package com.lhf.novel.exception;

import com.lhf.novel.util.ExceptionCode;
import com.lhf.novel.util.Result;
import io.jsonwebtoken.ExpiredJwtException;
import org.springframework.security.authentication.InsufficientAuthenticationException;
import org.springframework.security.authentication.LockedException;
import org.springframework.web.bind.annotation.ExceptionHandler;

/**
 * <p>异常捕获</p>
 *
 * @author zy 刘会发
 * @since 2020/3/12
 */
//@RestControllerAdvice
public class AllException<T> {

    /**
     * 捕获参数为空的异常
     *
     * @return
     */
    @ExceptionHandler(IllegalAccessException.class)
    public Result<T> illegalAccessException(IllegalAccessException exception) {
        return Result.other(ExceptionCode.NOT_NULL.getCode(), null, ExceptionCode.NOT_NULL.getMes());
    }

    /**
     * 用户被锁定被锁定
     *
     * @param exception
     * @return
     */
    @ExceptionHandler(LockedException.class)
    public Result<T> lockedException(LockedException exception) {
        return Result.other(ExceptionCode.USER_LOCKED.getCode(), null, ExceptionCode.USER_LOCKED.getMes());
    }

    /**
     * <p>用户未登录异常处理</p>
     *
     * @param exception
     * @return
     */
    @ExceptionHandler(InsufficientAuthenticationException.class)
    public Result<T> insufficientAuthenticationException(InsufficientAuthenticationException exception) {
        return Result.other(ExceptionCode.UN_LOGIN.getCode(), null, ExceptionCode.UN_LOGIN.getMes());
    }

    /**
     * 用户过期
     *
     * @param e
     * @return
     */
    @ExceptionHandler(ExpiredJwtException.class)
    public Result<T> expiredJwtException(ExpiredJwtException e) {
        return Result.other(4002, null, "用户已过期");
    }


    /**
     * 服务端其他异常
     *
     * @param exception
     * @return
     */
    @ExceptionHandler(Exception.class)
    public Result<T> exception(Exception exception) {
        return Result.failed();
    }


}
